package com.msmall.community.common.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.cache.CacheFactory;
import com.msmall.base.exception.AuthorityException;
import com.msmall.base.exception.DataNotFoundException;
import com.msmall.base.pager.MatchType;
import com.msmall.base.pager.PaginationSupport;
import com.msmall.base.service.impl.BaseManagerImpl;
import com.msmall.base.util.ConstantDB;
import com.msmall.base.util.Constants;
import com.msmall.community.bbs.model.Board;
import com.msmall.community.bbs.model.Topic;
import com.msmall.community.bbs.service.TopicManager;
import com.msmall.community.common.CascadeCollection;
import com.msmall.community.common.dao.CommendDao;
import com.msmall.community.common.model.Commend;
import com.msmall.community.common.service.CommendManager;
import com.msmall.community.common.util.CommendUtil;
import com.msmall.community.dpt.model.Segment;
import com.msmall.community.klg.model.Article;
import com.msmall.community.klg.model.SegmentArticle;
import com.msmall.community.klg.service.ArticleManager;
import com.msmall.community.question.model.Question;
import com.msmall.community.user.service.UserManager;

public class CommendManagerImpl extends BaseManagerImpl<Commend, Long> implements CommendManager{
	private static final Log logger = LogFactory.getLog(CommendManagerImpl.class);
	
	private CommendDao commendDao;
	
	private TopicManager topicManager;
	
	private UserManager userManager;
	
	private ArticleManager articleManager;
	
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public void setCommendDao(CommendDao commendDao) {
		this.commendDao = commendDao;
	}

	public void setTopicManager(TopicManager topicManager) {
		this.topicManager = topicManager;
	}

	public void setArticleManager(ArticleManager articleManager) {
		this.articleManager = articleManager;
	}

	public List<Commend> getAllAvailable2D() {
		return commendDao.getAllAvailable2D();
	}

	/**
	 * 刷新网站首页内容到缓存
	 */
	public void refreshSiteIndex(){
		List<Commend> babyIndexTopImages = getBabyIndexTopImages();
		List<Commend> babyIndexTopImagesCache = new ArrayList<Commend>();
		for(Commend commend : babyIndexTopImages){
			Topic topic = (Topic) topicManager.get(Topic.class, commend.getEntityId());
			commend.setObj(topic);
			commend.setImageAlt(topic.getTitle());
			babyIndexTopImagesCache.add(commend);
		}
		CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_INDEX_COMMEND_TOP_IMAGES, babyIndexTopImagesCache);
		
		List<Commend> babyIndexTopNews = getBabyIndexTopNews();
		List<Commend> babyIndexTopNewsCache = new ArrayList<Commend>();
		for(Commend commend : babyIndexTopNews){
			Topic topic = (Topic) topicManager.get(Topic.class, commend.getEntityId());
			commend.setObj(topic);
			babyIndexTopNewsCache.add(commend);
		}
		CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_INDEX_COMMEND_TOPNEWS, babyIndexTopNewsCache);
		
		List<Commend> babyIndexTopNewsBelow = getBabyIndexTopNewsBelow();
		List<Commend> babyIndexTopNewsBelowCache = new ArrayList<Commend>();
		for(Commend commend : babyIndexTopNewsBelow){
			Topic topic = (Topic) topicManager.get(Topic.class, commend.getEntityId());
			commend.setObj(topic);
			babyIndexTopNewsBelowCache.add(commend);
		}
		CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_INDEX_COMMEND_TOPNEWS_BELOW, babyIndexTopNewsBelowCache);
		
		List<Commend> babyIndexTopNewsBelowImage = getBabyIndexTopNewsBelowImage();
		List<Commend> babyIndexTopNewsBelowImageCache = new ArrayList<Commend>();
		for(Commend commend : babyIndexTopNewsBelowImage){
			Topic topic = (Topic) topicManager.get(Topic.class, commend.getEntityId());
			commend.setObj(topic);
			commend.setImageAlt(topic.getTitle());
			babyIndexTopNewsBelowImageCache.add(commend);
		}
		CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_INDEX_COMMEND_TOPNEWS_BELOW_IMAGE, babyIndexTopNewsBelowImageCache);
		
		List<Commend> babyIndexHotImage = getBabyIndexHotImage();
		List<Commend> babyIndexHotImageCache = new ArrayList<Commend>();
		for(Commend commend : babyIndexHotImage){
			Topic topic = (Topic) topicManager.get(Topic.class, commend.getEntityId());
			commend.setObj(topic);
			commend.setImageAlt(topic.getTitle());
			babyIndexHotImageCache.add(commend);
		}
		CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_INDEX_COMMEND_HOT_IMAGE, babyIndexHotImageCache);
		
		List<Commend> babyIndexBBSTopNews = getBabyIndexBBSTopNews();
		List<Commend> babyIndexBBSTopNewsCache = new ArrayList<Commend>();
		for(Commend commend : babyIndexBBSTopNews){
			Topic topic = (Topic) topicManager.get(Topic.class, commend.getEntityId());
			commend.setObj(topic);
			commend.setImageAlt(topic.getTitle());
			babyIndexBBSTopNewsCache.add(commend);
		}
		CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_INDEX_COMMEND_BBS_TOPNEWS, babyIndexBBSTopNewsCache);
		
		List<Commend> babyIndexBBSContentImage = getBabyIndexBBSContentImage();
		List<Commend> babyIndexBBSContentImageCache = new ArrayList<Commend>();
		for(Commend commend : babyIndexBBSContentImage){
			Topic topic = (Topic) topicManager.get(Topic.class, commend.getEntityId());
			commend.setObj(topic);
			commend.setImageAlt(topic.getTitle());
			babyIndexBBSContentImageCache.add(commend);
		}
		CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_INDEX_COMMEND_BBS_CONTENT_IMAGE, babyIndexBBSContentImageCache);
		
		
	}
	/**
	 * 网站首页大标题新闻下小标题图片，从BBS获得
	 */
	public List<Commend> getBabyIndexTopNewsBelowImage(){
		List<Commend> list = null;
		try{
			PaginationSupport page = new PaginationSupport();
			page.setClazz(Commend.class);
			page.setPageIndex(1);
			page.setPageSize(5);
			page.addFilter("commendDisplay", MatchType.EQ, ConstantDB.COMMEND_DISPLAY_22);
			page.addFilter("audit", MatchType.EQ, ConstantDB.AUDIT_PASS);
			page.addFilter("enabled", MatchType.EQ, ConstantDB.AVALIABLE);
			page.setOrderBy("commendTime");
			page.setOrder("desc");
			getPagination(page);
			list = page.getItems();
		}catch(Exception e){
			logger.error(e);
		}
		return list != null?list:new ArrayList<Commend>();
	}
	
	/**
	 * 网站首页Hot最右边图片，从BBS获得
	 */
	public List<Commend> getBabyIndexHotImage(){
		List<Commend> list = null;
		try{
			PaginationSupport page = new PaginationSupport();
			page.setClazz(Commend.class);
			page.setPageIndex(1);
			page.setPageSize(5);
			page.addFilter("commendDisplay", MatchType.EQ, ConstantDB.COMMEND_DISPLAY_23);
			page.addFilter("audit", MatchType.EQ, ConstantDB.AUDIT_PASS);
			page.addFilter("enabled", MatchType.EQ, ConstantDB.AVALIABLE);
			page.setOrderBy("commendTime");
			page.setOrder("desc");
			getPagination(page);
			list = page.getItems();
		}catch(Exception e){
			logger.error(e);
		}
		return list != null?list:new ArrayList<Commend>();
	}
	
	/**
	 * 网站首页BBS标题新闻，从BBS获得
	 */
	public List<Commend> getBabyIndexBBSTopNews(){
		List<Commend> list = null;
		try{
			PaginationSupport page = new PaginationSupport();
			page.setClazz(Commend.class);
			page.setPageIndex(1);
			page.setPageSize(5);
			page.addFilter("commendDisplay", MatchType.EQ, ConstantDB.COMMEND_DISPLAY_24);
			page.addFilter("audit", MatchType.EQ, ConstantDB.AUDIT_PASS);
			page.addFilter("enabled", MatchType.EQ, ConstantDB.AVALIABLE);
			page.setOrderBy("commendTime");
			page.setOrder("desc");
			getPagination(page);
			list = page.getItems();
		}catch(Exception e){
			logger.error(e);
		}
		return list != null?list:new ArrayList<Commend>();
	}
	
	/**
	 * 网站首页BBS图片，从BBS获得
	 */
	public List<Commend> getBabyIndexBBSContentImage(){
		List<Commend> list = null;
		try{
			PaginationSupport page = new PaginationSupport();
			page.setClazz(Commend.class);
			page.setPageIndex(1);
			page.setPageSize(5);
			page.addFilter("commendDisplay", MatchType.EQ, ConstantDB.COMMEND_DISPLAY_25);
			page.addFilter("audit", MatchType.EQ, ConstantDB.AUDIT_PASS);
			page.addFilter("enabled", MatchType.EQ, ConstantDB.AVALIABLE);
			page.setOrderBy("commendTime");
			page.setOrder("desc");
			getPagination(page);
			list = page.getItems();
		}catch(Exception e){
			logger.error(e);
		}
		return list != null?list:new ArrayList<Commend>();
	}
	
	/**
	 * 网站首页大标题新闻下小标题，从BBS获得
	 */
	public List<Commend> getBabyIndexTopNewsBelow(){
		List<Commend> list = null;
		try{
			PaginationSupport page = new PaginationSupport();
			page.setClazz(Commend.class);
			page.setPageIndex(1);
			page.setPageSize(5);
			page.addFilter("commendDisplay", MatchType.EQ, ConstantDB.COMMEND_DISPLAY_21);
			page.addFilter("audit", MatchType.EQ, ConstantDB.AUDIT_PASS);
			page.addFilter("enabled", MatchType.EQ, ConstantDB.AVALIABLE);
			page.setOrderBy("commendTime");
			page.setOrder("desc");
			getPagination(page);
			list = page.getItems();
		}catch(Exception e){
			logger.error(e);
		}
		return list != null?list:new ArrayList<Commend>();
	}
	/**
	 * 网站首页大标题新闻，从BBS获得
	 */
	public List<Commend> getBabyIndexTopNews(){
		List<Commend> list = null;
		try{
			PaginationSupport page = new PaginationSupport();
			page.setClazz(Commend.class);
			page.setPageIndex(1);
			page.setPageSize(5);
			page.addFilter("commendDisplay", MatchType.EQ, ConstantDB.COMMEND_DISPLAY_20);
			page.addFilter("audit", MatchType.EQ, ConstantDB.AUDIT_PASS);
			page.addFilter("enabled", MatchType.EQ, ConstantDB.AVALIABLE);
			page.setOrderBy("commendTime");
			page.setOrder("desc");
			getPagination(page);
			list = page.getItems();
		}catch(Exception e){
			logger.error(e);
		}
		return list != null?list:new ArrayList<Commend>();
	}
	/**
	 * 网站首页滚动图片，从BBS获得
	 */
	public List<Commend> getBabyIndexTopImages(){
		List<Commend> list = null;
		try{
			PaginationSupport page = new PaginationSupport();
			page.setClazz(Commend.class);
			page.setPageIndex(1);
			page.setPageSize(5);
			page.addFilter("commendDisplay", MatchType.EQ, ConstantDB.COMMEND_DISPLAY_19);
			page.addFilter("audit", MatchType.EQ, ConstantDB.AUDIT_PASS);
			page.addFilter("enabled", MatchType.EQ, ConstantDB.AVALIABLE);
			page.setOrderBy("commendTime");
			page.setOrder("desc");
			getPagination(page);
			list = page.getItems();
		}catch(Exception e){
			logger.error(e);
		}
		return list != null?list:new ArrayList<Commend>();
	}
	/**
	 *  BBS首页滚动图片：按推荐时间排序的5条新闻 commendType = constantdb.COMMEND_DISPLAY_1
	 */
	public List<Commend> getImageCommend(){
		List<Commend> list = null;
		try{
			PaginationSupport page = new PaginationSupport();
			page.setClazz(Commend.class);
			page.setPageIndex(1);
			page.setPageSize(5);
			page.addFilter("commendDisplay", MatchType.EQ, ConstantDB.COMMEND_DISPLAY_1);
			page.addFilter("audit", MatchType.EQ, ConstantDB.AUDIT_PASS);
			page.addFilter("enabled", MatchType.EQ, ConstantDB.AVALIABLE);
			page.setOrderBy("commendTime");
			page.setOrder("desc");
			getPagination(page);
			list = page.getItems();
		}catch(Exception e){
			logger.error(e);
		}
		return list != null?list:new ArrayList<Commend>();
	}
	
	/**
	 *  BBS首页标题新闻：按推荐时间排序的2条新闻 constantdb.COMMEND_DISPLAY_2
	 */
	public List<Commend> getTopNewsCommend(){
		List<Commend> list = null;
		try{
			PaginationSupport page = new PaginationSupport();
			page.setClazz(Commend.class);
			page.setPageIndex(1);
			page.setPageSize(2);
			page.addFilter("commendDisplay", MatchType.EQ, ConstantDB.COMMEND_DISPLAY_2);
			page.addFilter("audit", MatchType.EQ, ConstantDB.AUDIT_PASS);
			page.addFilter("enabled", MatchType.EQ, ConstantDB.AVALIABLE);
			page.setOrderBy("commendTime");
			page.setOrder("desc");
			getPagination(page);
			list = page.getItems();
		}catch(Exception e){
			logger.error(e);
		}
		return list != null?list:new ArrayList<Commend>();
	}
	
	/**
	 *  BBS推荐话题：按推荐时间排序的10条 constantdb.COMMEND_DISPLAY_3
	 */
	public List<Commend> getHotTopicCommend(){
		List<Commend> list = null;
		try{
			PaginationSupport page = new PaginationSupport();
			page.setClazz(Commend.class);
			page.setPageIndex(1);
			page.setPageSize(10);
			page.addFilter("commendDisplay", MatchType.EQ, ConstantDB.COMMEND_DISPLAY_3);
			page.addFilter("audit", MatchType.EQ, ConstantDB.AUDIT_PASS);
			page.addFilter("enabled", MatchType.EQ, ConstantDB.AVALIABLE);
			page.setOrderBy("commendTime");
			page.setOrder("desc");
			getPagination(page);
			list = page.getItems();
		}catch(Exception e){
			logger.error(e);
		}
		return list != null?list:new ArrayList<Commend>();
	}
	
	/**
	 *  BBS首页各大版区推荐图片：按推荐时间排序的1条 constantdb.COMMEND_DISPLAY_4
	 */
	public List<Commend> getBBSIndexEachBoardImageCommend(){
		List<Commend> list = null;
		try{
			PaginationSupport page = new PaginationSupport();
			page.setClazz(Commend.class);
			page.setPageIndex(1);
			page.setPageSize(10);
			page.addFilter("commendDisplay", MatchType.EQ, ConstantDB.COMMEND_DISPLAY_4);
			page.addFilter("audit", MatchType.EQ, ConstantDB.AUDIT_PASS);
			page.addFilter("enabled", MatchType.EQ, ConstantDB.AVALIABLE);
			page.setOrderBy("commendTime");
			page.setOrder("desc");
			getPagination(page);
			list = page.getItems();
		}catch(Exception e){
			logger.error(e);
		}
		return list != null?list:new ArrayList<Commend>();
	}
	
	/**
	 *  BBS首页各大版区推荐图片下的帖子列表：按推荐时间排序的10条 constantdb.COMMEND_DISPLAY_5
	 *   从BBS TOPIC 获得
	 */
	public List<Commend> getBBSIndexEachBoardImageBelowCommend(){
		List<Commend> list = null;
		try{
			PaginationSupport page = new PaginationSupport();
			page.setClazz(Commend.class);
			page.setPageIndex(1);
			page.setPageSize(10);
			page.addFilter("commendDisplay", MatchType.EQ, ConstantDB.COMMEND_DISPLAY_5);
			page.addFilter("audit", MatchType.EQ, ConstantDB.AUDIT_PASS);
			page.addFilter("enabled", MatchType.EQ, ConstantDB.AVALIABLE);
			page.setOrderBy("commendTime");
			page.setOrder("desc");
			getPagination(page);
			list = page.getItems();
		}catch(Exception e){
			logger.error(e);
		}
		return list != null?list: new ArrayList<Commend>();
	}
	/**
	 *  获得推荐的标题新闻下面的小推荐：按推荐时间排序的6条新闻 commendType = constantdb.COMMEND_DISPLAY_13
	 */
	public List<Commend> getTopNewsBelowCommend(){
		List<Commend> list = null;
		try{
			PaginationSupport page = new PaginationSupport();
			page.setClazz(Commend.class);
			page.setPageIndex(1);
			page.setPageSize(6);
			page.addFilter("commendDisplay", MatchType.EQ, ConstantDB.COMMEND_DISPLAY_13);
			page.addFilter("audit", MatchType.EQ, ConstantDB.AUDIT_PASS);
			page.addFilter("enabled", MatchType.EQ, ConstantDB.AVALIABLE);
			page.setOrderBy("commendTime");
			page.setOrder("desc");
			getPagination(page);
			list = page.getItems();
		}catch(Exception e){
			logger.error(e);
		}
		return list != null?list:new ArrayList<Commend>();
	}
	/**
	 *  获得推荐的公告：按推荐时间排序的5条新闻 commendType = constantdb.COMMEND_DISPLAY_14
	 */
	public List<Commend> getAnnounceCommend(){
		List<Commend> list = null;
		try{
			PaginationSupport page = new PaginationSupport();
			page.setClazz(Commend.class);
			page.setPageIndex(1);
			page.setPageSize(5);
			page.addFilter("commendDisplay", MatchType.EQ, ConstantDB.COMMEND_DISPLAY_14);
			page.addFilter("audit", MatchType.EQ, ConstantDB.AUDIT_PASS);
			page.addFilter("enabled", MatchType.EQ, ConstantDB.AVALIABLE);
			page.setOrderBy("commendTime");
			page.setOrder("desc");
			getPagination(page);
			list = page.getItems();
		}catch(Exception e){
			logger.error(e);
		}
		return list != null?list:new ArrayList<Commend>();
	}
	
	/**
	 *  BBS首页各大版区标题新闻：按推荐时间排序的2条新闻 commendType = constantdb.COMMEND_DISPLAY_15
	 */
	public List<Commend> getBBSIndexEachBoardTopNewsCommend(){
		List<Commend> list = null;
		try{
			PaginationSupport page = new PaginationSupport();
			page.setClazz(Commend.class);
			page.setPageIndex(1);
			page.setPageSize(5);
			page.addFilter("commendDisplay", MatchType.EQ, ConstantDB.COMMEND_DISPLAY_15);
			page.addFilter("audit", MatchType.EQ, ConstantDB.AUDIT_PASS);
			page.addFilter("enabled", MatchType.EQ, ConstantDB.AVALIABLE);
			page.setOrderBy("commendTime");
			page.setOrder("desc");
			getPagination(page);
			list = page.getItems();
		}catch(Exception e){
			logger.error(e);
		}
		return list != null?list:new ArrayList<Commend>();
	}
	/**
	 *  BBS首页各大版区标题新闻下的帖子列表：按推荐时间排序的6条新闻 commendType = constantdb.COMMEND_DISPLAY_16
	 */
	public List<Commend> getBBSIndexEachBoardTopNewsBelowCommend(){
		List<Commend> list = null;
		try{
			PaginationSupport page = new PaginationSupport();
			page.setClazz(Commend.class);
			page.setPageIndex(1);
			page.setPageSize(5);
			page.addFilter("commendDisplay", MatchType.EQ, ConstantDB.COMMEND_DISPLAY_16);
			page.addFilter("audit", MatchType.EQ, ConstantDB.AUDIT_PASS);
			page.addFilter("enabled", MatchType.EQ, ConstantDB.AVALIABLE);
			page.setOrderBy("commendTime");
			page.setOrder("desc");
			getPagination(page);
			list = page.getItems();
		}catch(Exception e){
			logger.error(e);
		}
		return list != null?list:new ArrayList<Commend>();
	}
	
	/**
	 *问题首页的推荐
	 */
	public List<Commend> getQuestionImages(){
		List<Commend> list = null;
		try{
			PaginationSupport page = new PaginationSupport();
			page.setClazz(Commend.class);
			page.setPageIndex(1);
			page.setPageSize(10);
			page.addFilter("commendDisplay", MatchType.EQ, ConstantDB.COMMEND_DISPLAY_6);
			page.addFilter("audit", MatchType.EQ, ConstantDB.AUDIT_PASS);
			page.addFilter("enabled", MatchType.EQ, ConstantDB.AVALIABLE);
			page.setOrderBy("commendTime");
			page.setOrder("desc");
			getPagination(page);
			list = page.getItems();
		}catch(Exception e){
			logger.error(e);
		}
		return list != null?list:new ArrayList<Commend>();
	}
	/**
	 *问题首页推荐问题
	 */
	public List<Commend> getQuestionHot(){
		List<Commend> list = null;
		try{
			PaginationSupport page = new PaginationSupport();
			page.setClazz(Commend.class);
			page.setPageIndex(1);
			page.setPageSize(20);
			page.addFilter("commendDisplay", MatchType.EQ, ConstantDB.COMMEND_DISPLAY_7);
			page.addFilter("audit", MatchType.EQ, ConstantDB.AUDIT_PASS);
			page.addFilter("enabled", MatchType.EQ, ConstantDB.AVALIABLE);
			page.setOrderBy("commendTime");
			page.setOrder("desc");
			getPagination(page);
			list = page.getItems();
		}catch(Exception e){
			logger.error(e);
		}
		return list != null?list:new ArrayList<Commend>();
	}
	/**
	 *问题 妈妈关系的问题
	 */
	public List<Commend> getQuestionMother(){
		List<Commend> list = null;
		try{
			PaginationSupport page = new PaginationSupport();
			page.setClazz(Commend.class);
			page.setPageIndex(1);
			page.setPageSize(10);
			page.addFilter("commendDisplay", MatchType.EQ, ConstantDB.COMMEND_DISPLAY_8);
			page.addFilter("audit", MatchType.EQ, ConstantDB.AUDIT_PASS);
			page.addFilter("enabled", MatchType.EQ, ConstantDB.AVALIABLE);
			page.setOrderBy("commendTime");
			page.setOrder("desc");
			getPagination(page);
			list = page.getItems();
		}catch(Exception e){
			logger.error(e);
		}
		return list != null?list:new ArrayList<Commend>();
	}
	/**
	 *问题 宝宝关心的问题
	 */
	public List<Commend> getQuestionBaby(){
		List<Commend> list = null;
		try{
			PaginationSupport page = new PaginationSupport();
			page.setClazz(Commend.class);
			page.setPageIndex(1);
			page.setPageSize(10);
			page.addFilter("commendDisplay", MatchType.EQ, ConstantDB.COMMEND_DISPLAY_9);
			page.addFilter("audit", MatchType.EQ, ConstantDB.AUDIT_PASS);
			page.addFilter("enabled", MatchType.EQ, ConstantDB.AVALIABLE);
			page.setOrderBy("commendTime");
			page.setOrder("desc");
			getPagination(page);
			list = page.getItems();
		}catch(Exception e){
			logger.error(e);
		}
		return list != null?list:new ArrayList<Commend>();
	}
	
	/**
	 * 刷新BBS的所有推荐到缓存
	 */
	public void refreshBBS(){
		List<Commend> commends1 = getImageCommend();
		List<Commend> commends2 = getTopNewsCommend();
		List<Commend> commends3 = getHotTopicCommend();
		List<Commend> commends6 = getTopNewsBelowCommend();
		List<Commend> commends7 = getAnnounceCommend();
		
		List<Topic> topics2 = new ArrayList<Topic>();
		List<Topic> topics3 = new ArrayList<Topic>();
		List<Topic> topics6 = new ArrayList<Topic>();
		List<Topic> topics7 = new ArrayList<Topic>();
		
		for(int i = 0; i < commends1.size(); i++){
			Commend commend = commends1.get(i);
			Long entityId = commend.getEntityId();
			Topic topic = (Topic) topicManager.get(Topic.class, entityId);
			commend.setImageAlt(topic.getTitle());
		}
		CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_BBS_COMMEND_IMAGE_LIST, commends1);
		
		for(int i = 0; i < commends2.size(); i++){
			Commend commend = commends2.get(i);
			Long entityId = commend.getEntityId();
			Topic topic = (Topic) topicManager.get(Topic.class, entityId);
			topics2.add(topic);
		}
		CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_BBS_COMMEND_TOPNEWS_LIST, topics2);
		
		for(int i = 0; i < commends3.size(); i++){
			Commend commend = commends3.get(i);
			Long entityId = commend.getEntityId();
			Topic topic = (Topic) topicManager.get(Topic.class, entityId);
			topics3.add(topic);
		}
		CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_BBS_COMMEND_HOTTOPIC_LIST, topics3);
		
		List<Commend> commends4 = getBBSIndexEachBoardImageCommend();
		Map<Long, List<Commend>> map4 = new HashMap<Long, List<Commend>>();
		for(int i = 0; i < commends4.size(); i++){
			Commend commend = commends4.get(i);
			Long entityId = commend.getEntityId();
			Topic topic = (Topic) topicManager.get(Topic.class, entityId);
			commend.setImageAlt(topic.getTitle());
			Long fitid = getFitableBoard(topic.getBoardid());
			
			List<Commend> lists = map4.get(fitid);
			if(lists == null){
				lists = new ArrayList<Commend>();
				map4.put(fitid, lists);
			}
			lists.add(commend);
		}
		CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_BBS_COMMEND_INDEX_EACH_BOARD_IMAGE_LIST, map4);
		
		List<Commend> commends5 = getBBSIndexEachBoardImageBelowCommend();
		Map<Long, List<Topic>> map5 = new HashMap<Long, List<Topic>>();
		for(int i = 0; i < commends5.size(); i++){
			Commend commend = commends5.get(i);
			Long entityId = commend.getEntityId();
			Topic topic = (Topic) topicManager.get(Topic.class, entityId);
			
			Long fitid = getFitableBoard(topic.getBoardid());
			
			List<Topic> lists = map5.get(fitid);
			if(lists == null){
				lists = new ArrayList<Topic>();
				map5.put(fitid, lists);
			}
			lists.add(topic);
		}
		CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_BBS_COMMEND_INDEX_EACH_BOARD_IMAGE_BELOW_LIST, map5);
		
		for(int i = 0; i < commends6.size(); i++){
			Commend commend = commends6.get(i);
			Long entityId = commend.getEntityId();
			Topic topic = (Topic) topicManager.get(Topic.class, entityId);
			topics6.add(topic);
		}
		CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_BBS_COMMEND_TOPNEWS_BELOW_LIST, topics6);
		
		for(int i = 0; i < commends7.size(); i++){
			Commend commend = commends7.get(i);
			Long entityId = commend.getEntityId();
			Topic topic = (Topic) topicManager.get(Topic.class, entityId);
			topics7.add(topic);
		}
		CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_BBS_COMMEND_ANNOUNCE_LIST, topics7);
		
		List<Commend> commends8 = getBBSIndexEachBoardTopNewsCommend();
		Map<Long, List<Topic>> map8 = new HashMap<Long, List<Topic>>();
		for(int i = 0; i < commends8.size(); i++){
			Commend commend = commends8.get(i);
			Long entityId = commend.getEntityId();
			Topic topic = (Topic) topicManager.get(Topic.class, entityId);
			
			Long fitid = getFitableBoard(topic.getBoardid());
			
			List<Topic> lists = map8.get(fitid);
			if(lists == null){
				lists = new ArrayList<Topic>();
				map8.put(fitid, lists);
			}
			lists.add(topic);
		}
		CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_BBS_COMMEND_INDEX_EACH_BOARD_TOPNEWS_LIST, map8);
		
		List<Commend> commends9 = getBBSIndexEachBoardTopNewsBelowCommend();
		Map<Long, List<Topic>> map9 = new HashMap<Long, List<Topic>>();
		for(int i = 0; i < commends9.size(); i++){
			Commend commend = commends9.get(i);
			Long entityId = commend.getEntityId();
			Topic topic = (Topic) topicManager.get(Topic.class, entityId);

			Long fitid = getFitableBoard(topic.getBoardid());
			
			List<Topic> lists = map9.get(fitid);
			if(lists == null){
				lists = new ArrayList<Topic>();
				map9.put(fitid, lists);
			}
			lists.add(topic);
		}
		CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_BBS_COMMEND_INDEX_EACH_BOARD_TOPNEWS_BELOW_LIST, map9);
		
	}
	/**
	 * 获得最顶层BOARDID
	 */
	public Long getFitableBoard(Long boardid){
		CascadeCollection<Board> cs = (CascadeCollection<Board>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_BBS_BOARD_CASCADECOLLECTION);
		CascadeCollection<Board> current = cs.getAllDatas().get(boardid);
		if(current.getCurrent().getParentId().compareTo(ConstantDB.BBS_BOARD_ID_TOP) == 0){
			return current.getCurrent().getBoardId();
		}
		return current.getFirstParent().getCurrent().getBoardId();
	}
	public Long getFitableSegment(Long type){
		CascadeCollection<Segment> cs = (CascadeCollection<Segment>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_SEGMENT_CASCADECOLLECTION);
		CascadeCollection<Segment> current = cs.getAllDatas().get(type);
		if(current.getCurrent().getParentid().compareTo(ConstantDB.SEGMENT_ID_QUESTION_NAV) == 0){
			return current.getCurrent().getSegmentid();
		}
		return current.getFirstParent().getCurrent().getSegmentid();
	}
	/**
	 * 刷新问答的所有推荐到缓存
	 */
	public void refreshQuestion(){
		List<Commend> commends6 = getQuestionImages();
		
		List<Commend> questionImage = new ArrayList<Commend>();
		for(int i = 0; i < commends6.size(); i++){
			Commend commend = commends6.get(i);
			Long entityId = commend.getEntityId();
			Question q = (Question) get(Question.class, entityId);
			commend.setObj(q);
			questionImage.add(commend);
		}
		CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_QUESTION_COMMEND_IMAGES, questionImage);
		
		List<Commend> commends7 = getQuestionHot();
		Map<Long, List<Commend>> questionMaps = new HashMap<Long, List<Commend>>();
		List<Commend> questionHot;
		for(int i = 0; i < commends7.size(); i++){
			Commend commend = commends7.get(i);
			Long entityId = commend.getEntityId();
			Question q = (Question) get(Question.class, entityId);
			Long segid = getFitableSegment(q.getType());
			commend.setObj(q);
			
			questionHot = questionMaps.get(segid);
			if(questionHot == null){
				questionHot = new ArrayList<Commend>();
				questionMaps.put(segid, questionHot);
			}
			questionHot.add(commend);
		}
		CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_QUESTION_COMMEND_HOT, questionMaps);
		
		List<Commend> commends8 = getQuestionMother();
		List<Commend> questionMother = new ArrayList<Commend>();
		for(int i = 0; i < commends8.size(); i++){
			Commend commend = commends8.get(i);
			Long entityId = commend.getEntityId();
			Question q = (Question) get(Question.class, entityId);
			commend.setObj(q);
			questionMother.add(commend);
		}
		CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_QUESTION_COMMEND_MOTHER, questionMother);
		
		List<Commend> commends9 = getQuestionBaby();
		List<Commend> questionBaby = new ArrayList<Commend>();
		for(int i = 0; i < commends9.size(); i++){
			Commend commend = commends9.get(i);
			Long entityId = commend.getEntityId();
			Question q = (Question) get(Question.class, entityId);
			commend.setObj(q);
			questionBaby.add(commend);
		}
		CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_QUESTION_COMMEND_BABY, questionBaby);
		
	}
	
	/**
	 * 刷新知识的所有推荐到缓存
	 */
	public void refreshKlg(){
		try{
			CascadeCollection<Segment> cs = (CascadeCollection<Segment>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_SEGMENT_CASCADECOLLECTION);
			/**
			 *知识主Segment首页推荐图片
			 */
			Map<Long,Commend> klgCommendImages = new HashMap<Long, Commend>();
			List<Commend> commends1 = getKlgCommendImages();
			
			for(int i = 0; i < commends1.size(); i++){
				Commend commend = commends1.get(i);
				Long entityId = commend.getEntityId();
				Article q = (Article) get(Article.class, entityId);
				commend.setObj(q);
				SegmentArticle sa = articleManager.getSegmentByArticleid(entityId);
				
				CascadeCollection<Segment> current = cs.getAllDatas().get(sa.getSegmentid());
				CascadeCollection<Segment> parent = current.getFirstParent();
				klgCommendImages.put(parent.getCurrent().getSegmentid(), commend);
			}
			CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_KLG_COMMEND_IMAGES, klgCommendImages);
			
			/**
			 *知识主Segment首页推荐大标题
			 */
			Map<Long, List<Commend>> klgCommendTopNews = new HashMap<Long, List<Commend>>();
			List<Commend> commends2 = getKlgCommendTopNews();
			
			for(int i = 0; i < commends2.size(); i++){
				Commend commend = commends2.get(i);
				Long entityId = commend.getEntityId();
				Article q = (Article) get(Article.class, entityId);
				commend.setObj(q);
				SegmentArticle sa = articleManager.getSegmentByArticleid(entityId);
				
				CascadeCollection<Segment> current = cs.getAllDatas().get(sa.getSegmentid());
				CascadeCollection<Segment> parent = current.getFirstParent();
				List<Commend> commends = klgCommendTopNews.get(parent.getCurrent().getSegmentid());
				if(commends == null){
					commends = new ArrayList<Commend>();
					klgCommendTopNews.put(parent.getCurrent().getSegmentid(), commends);
				}
				commends.add(commend);
			}
			CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_KLG_COMMEND_TOP_NEWS, klgCommendTopNews);
			
			/**
			 *知识主Segment首页各子Segment的推荐
			 */
			Map<Long,Commend> klgCommendSubSegmentImages = new HashMap<Long, Commend>();
			List<Commend> commends3 = getKlgCommendSubSegmentImages();
			
			for(int i = 0; i < commends3.size(); i++){
				Commend commend = commends3.get(i);
				Long entityId = commend.getEntityId();
				Article q = (Article) get(Article.class, entityId);
				commend.setObj(q);
				SegmentArticle sa = articleManager.getSegmentByArticleid(entityId);
				klgCommendSubSegmentImages.put(sa.getSegmentid(), commend);
			}
			CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_KLG_COMMEND_SUBSEGMENT_IMAGES, klgCommendSubSegmentImages);
			
			/**
			 * 知识中关于妈妈的推荐(网站首页使用)
			 */
			List<Commend> klgCommendMother = getKlgCommendMother();
			for(int i = 0; i < klgCommendMother.size(); i++){
				Commend commend = klgCommendMother.get(i);
				Long entityId = commend.getEntityId();
				Article q = (Article) get(Article.class, entityId);
				commend.setObj(q);
			}
			CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_KLG_COMMEND_MOTHER, klgCommendMother);
			
			/**
			 * 知识中关于宝宝的推荐(网站首页使用)
			 */
			List<Commend> klgCommendBaby = getKlgCommendBaby();
			for(int i = 0; i < klgCommendBaby.size(); i++){
				Commend commend = klgCommendBaby.get(i);
				Long entityId = commend.getEntityId();
				Article q = (Article) get(Article.class, entityId);
				commend.setObj(q);
			}
			CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_KLG_COMMEND_BABY, klgCommendBaby);
			
		}catch(Exception e){
			log.debug(e);
		}
	}
	
	/**
	 *知识主Segment首页推荐图片
	 */
	public List<Commend> getKlgCommendImages(){
		List<Commend> list = null;
		try{
			PaginationSupport page = new PaginationSupport();
			page.setClazz(Commend.class);
			page.setPageIndex(1);
			page.setPageSize(10);
			page.setAutoCount(false);
			page.addFilter("commendDisplay", MatchType.EQ, ConstantDB.COMMEND_DISPLAY_10);
			page.addFilter("audit", MatchType.EQ, ConstantDB.AUDIT_PASS);
			page.addFilter("enabled", MatchType.EQ, ConstantDB.AVALIABLE);
			page.setOrderBy("commendTime");
			page.setOrder("desc");
			getPagination(page);
			list = page.getItems();
		}catch(Exception e){
			logger.error(e);
		}
		return list != null?list:new ArrayList<Commend>();
	}
	/**
	 *知识主Segment首页推荐大标题
	 */
	public List<Commend> getKlgCommendTopNews(){
		List<Commend> list = null;
		try{
			PaginationSupport page = new PaginationSupport();
			page.setClazz(Commend.class);
			page.setPageIndex(1);
			page.setPageSize(10);
			page.setAutoCount(false);
			page.addFilter("commendDisplay", MatchType.EQ, ConstantDB.COMMEND_DISPLAY_11);
			page.addFilter("audit", MatchType.EQ, ConstantDB.AUDIT_PASS);
			page.addFilter("enabled", MatchType.EQ, ConstantDB.AVALIABLE);
			page.setOrderBy("commendTime");
			page.setOrder("desc");
			getPagination(page);
			list = page.getItems();
		}catch(Exception e){
			logger.error(e);
		}
		return list != null?list:new ArrayList<Commend>();
	}
	/**
	 *知识主Segment首页各子Segment的推荐
	 */
	public List<Commend> getKlgCommendSubSegmentImages(){
		List<Commend> list = null;
		try{
			PaginationSupport page = new PaginationSupport();
			page.setClazz(Commend.class);
			page.setPageIndex(1);
			page.setPageSize(10);
			page.setAutoCount(false);
			page.addFilter("commendDisplay", MatchType.EQ, ConstantDB.COMMEND_DISPLAY_12);
			page.addFilter("audit", MatchType.EQ, ConstantDB.AUDIT_PASS);
			page.addFilter("enabled", MatchType.EQ, ConstantDB.AVALIABLE);
			page.setOrderBy("commendTime");
			page.setOrder("desc");
			getPagination(page);
			list = page.getItems();
		}catch(Exception e){
			logger.error(e);
		}
		return list != null?list:new ArrayList<Commend>();
	}
	/**
	 * 知识中关于妈妈的推荐(网站首页使用)
	 */
	public List<Commend> getKlgCommendMother(){
		List<Commend> list = null;
		try{
			PaginationSupport page = new PaginationSupport();
			page.setClazz(Commend.class);
			page.setPageIndex(1);
			page.setPageSize(10);
			page.setAutoCount(false);
			page.addFilter("commendDisplay", MatchType.EQ, ConstantDB.COMMEND_DISPLAY_17);
			page.addFilter("audit", MatchType.EQ, ConstantDB.AUDIT_PASS);
			page.addFilter("enabled", MatchType.EQ, ConstantDB.AVALIABLE);
			page.setOrderBy("commendTime");
			page.setOrder("desc");
			getPagination(page);
			list = page.getItems();
		}catch(Exception e){
			logger.error(e);
		}
		return list != null?list:new ArrayList<Commend>();
	}
	
	/**
	 * 知识中关于宝宝的推荐(网站首页使用)
	 */
	public List<Commend> getKlgCommendBaby(){
		List<Commend> list = null;
		try{
			PaginationSupport page = new PaginationSupport();
			page.setClazz(Commend.class);
			page.setPageIndex(1);
			page.setPageSize(10);
			page.setAutoCount(false);
			page.addFilter("commendDisplay", MatchType.EQ, ConstantDB.COMMEND_DISPLAY_18);
			page.addFilter("audit", MatchType.EQ, ConstantDB.AUDIT_PASS);
			page.addFilter("enabled", MatchType.EQ, ConstantDB.AVALIABLE);
			page.setOrderBy("commendTime");
			page.setOrder("desc");
			getPagination(page);
			list = page.getItems();
		}catch(Exception e){
			logger.error(e);
		}
		return list != null?list:new ArrayList<Commend>();
	}
	//TODO 默认审核了，审核人是当前推荐的
	public void createCommend(Commend commend, Long userid) {
		
		// BBS
		if(ConstantDB.COMMEND_TYPE_BBS_TOPIC.compareTo(commend.getCommendType()) == 0){
			try {
				topicManager.setCommend(commend.getEntityId(), userid);
			} catch (DataNotFoundException e) {
				logger.debug(e.getMessage());
			} catch (AuthorityException e) {
				logger.debug(e.getMessage());
			} catch(Exception e){
				logger.debug(e.getMessage());
			}
		}
		//SPACE
		if(ConstantDB.COMMEND_TYPE_SPACE_BLOG.compareTo(commend.getCommendType()) == 0){
			//TODO commend space
		}
		Commend old = commendDao.getByTypeAndId(commend.getCommendType(), commend.getEntityId());
		if(old == null){
			commend.setAudit(ConstantDB.AUDIT_PASS);
			commend.setAuditor(userid);
			commend.setAuditTime(new Date());
			commend.setCommender(userid);
			commend.setCommendLevel(ConstantDB.COMMEND_LEVEL_TOPEST);
			commend.setCommendTime(new Date());
			commend.setComments("");
			commend.setEnabled(ConstantDB.AVALIABLE);
			commend.setCommendDisplay(ConstantDB.COMMEND_DISPLAY_0);
			commend.setReason("");
			commend.setUrl("");
			commendDao.save(commend);
		}else{
			old.setAuditor(userid);
			old.setAuditTime(new Date());
			old.setCommender(userid);
			old.setCommendLevel(ConstantDB.COMMEND_LEVEL_TOPEST);
			old.setCommendTime(new Date());
			old.setEnabled(ConstantDB.AVALIABLE);
			commend.setCommendDisplay(ConstantDB.COMMEND_DISPLAY_0);
			commendDao.save(old);
		}
	}
	/**
	 * 后台管理用户删除一个推荐
	 */
	public void removeCommend(Long commendid, Long userid){
		commendDao.remove(Commend.class, commendid);
	}
	/**
	 * 后台管理用户删除一个推荐
	 */
	public void removeCommends(String commendids, Long userid){
		String[] ids = commendids.split(",");
		for(int i=0;i<ids.length;i++){
			if(!StringUtils.isEmpty(ids[i])){
				removeCommend(Long.parseLong(ids[i]), userid);
			}
		}
	}
	public void removeCommend(Short type, Long eid, Long userid){
		try{
			// BBS
			if(ConstantDB.COMMEND_TYPE_BBS_TOPIC.compareTo(type) == 0){
				topicManager.deleteCommend(eid, userid);
			}
			//SPACE
			if(ConstantDB.COMMEND_TYPE_SPACE_BLOG.compareTo(type) == 0){
				//TODO commend space
			}
			//Question
			if(ConstantDB.COMMEND_TYPE_QUESTION.compareTo(type) == 0){
				Commend commend = getByTypeAndId(type, eid);
				commendDao.remove(commend);
			}
			/*
			Commend commend = commendDao.getByTypeAndId(type, eid);
			if(commend == null)
				return;
			commend.setEnabled(ConstantDB.UNAVALIABLE);
			commendDao.save(commend);
			*/
		}catch(Exception e){
			logger.error(e);
		}
	}
	/**
	 * 后台管理员管理推荐，包括审核，显示位置
	 * @param commend
	 * @param userid
	 */
	public void auditCommend(File image,String filename,Commend commend, Long userid, String uat){
		try{
			if(image != null){
				String[] pathinfo = CommendUtil.saveImage(image, filename, commend.getCommendId(), uat);
				commend.setImageName(pathinfo[1]);
				commend.setImagePath(pathinfo[0]);
			}
			commend.setAuditor(userid);
			commend.setAuditTime(new Date());
			commendDao.save(commend);
		}catch(Exception e){
			logger.error(e);
		}
	}
	/**
	 * @param type ContantDB.COMMEND_TYPE_BBS_TOPIC  COMMEND_TYPE_QUESTION ..
	 * @param entityId Topic/Blog/Question 对应ID
	 */
	public Commend getByTypeAndId(Short type, Long eid) throws Exception{
		try{
			return commendDao.getByTypeAndId(type, eid);
		}catch(Exception e){
			logger.error(e);
			throw e;
		}
	}
	
}
